package com.dfdchem.imd.kaiwu.activity;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.bumptech.glide.Glide;
import com.dfdchem.imd.kaiwu.R;
import com.dfdchem.imd.kaiwu.activity.base.BaseActivityUM;
import com.dfdchem.imd.kaiwu.constant.RouterPath;
import com.dfdchem.imd.kaiwu.manager.GlideManager;

import java.io.File;
import java.util.Timer;
import java.util.TimerTask;

import zuo.biao.library.util.Log;
import zuo.biao.library.util.StringUtil;

/**
 * 闪屏广告Activity
 * @author yqq
 */

@Route(path = RouterPath.SPLASH_AD_ACTIVITY)
public class SplashAdActivity extends BaseActivityUM implements View.OnClickListener {
	private static final String TAG = "SplashAdActivity";

	@Autowired
	String imageUrl;

	@Autowired
	String imagePath;

	@Autowired
	String url;

	private Button skip;
	private ImageView imageView;

	private Timer timer;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Log.d(TAG, this.getClass().getName() + " onCreate");

		setContentView(R.layout.activity_splash_ad);

		Log.d(TAG, "imageUrl:" + imageUrl);
		Log.d(TAG, "imagePath:" + imagePath);
		Log.d(TAG, "url:" + url);

		initView();
		initData();
		initEvent();

	}

	private void initView(){
		skip = findViewById(R.id.skip);
		imageView = findViewById(R.id.imageView);
	}

	private void initData(){
		if (!StringUtil.isEmpty(imagePath)) {
			File file = new File(imagePath);
			Glide.with(this).load(file).into(imageView);
		} else if(!StringUtil.isEmpty(imageUrl)) {
			new GlideManager(this).load(imageView, imageUrl);
		} else {
			toActivity(RouterPath.MAIN_ACTIVITY, false, android.R.anim.fade_in, android.R.anim.fade_out);
			finish();
		}



		final int[] counter = {3};
		// 搜索框内容定时更新
		Handler handler = new Handler() {
			public void handleMessage(Message msg) {
				skip.setText("跳过" + (counter[0]-- + ""));
				if (counter[0] == 0) {
					new Handler().postDelayed(new Runnable() {
						@Override
						public void run() {
							toActivity(RouterPath.MAIN_ACTIVITY, false, android.R.anim.fade_in, android.R.anim.fade_out);
							finish();
						}
					}, 250);
				}

				super.handleMessage(msg);
			}
		};

		TimerTask task = new TimerTask() {
			@Override
			public void run() {
				handler.sendMessage(new Message());
			}
		};

		timer = new Timer();
		timer.schedule(task, 0, 1000);
	}

	private void initEvent(){
		imageView.setOnClickListener(this);
	}

	public void onSkipClick(View v) {
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				toActivity(RouterPath.MAIN_ACTIVITY, false, android.R.anim.fade_in, android.R.anim.fade_out);
				finish();
			}
		}, 250);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.imageView:
				timer.cancel();
				new Handler().postDelayed(new Runnable() {
					@Override
					public void run() {
						ARouter.getInstance().build(RouterPath.MAIN_ACTIVITY)
								.withString("url", url)
								.navigation(SplashAdActivity.this);
						finish();
					}
				}, 250);
				break;
		}
	}

	@Override
	protected void onDestroy() {
		timer.cancel();
		timer = null;
		super.onDestroy();
	}
}